package cn.cc.goods.controller.controller;

import cn.cc.common.beanCopy.utils.BeanCopierUtil;
import cn.cc.common.result.utils.RestResult;
import cn.cc.common.result.utils.RestResultEmum;
import cn.cc.goods.controller.dto.AddGoodsSpuDTO;
import cn.cc.goods.controller.vo.GetGoodsSpuVO;
import cn.cc.goods.service.GoodsSpuService;
import cn.cc.goods.service.bo.AddGoodsSpuBO;
import cn.cc.goods.service.domain.GetGoodsSpuDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: smail-mi-cloud
 * @description:
 * @author: Mr.WuChao
 * @create: 2021-07-16 16:34
 **/
@RestController
@RequestMapping("/goodsSpu")
public class GoodsSpuController {
    @Autowired
    private GoodsSpuService goodsSpuService;

    @PostMapping("/add")
    RestResult<Void> addGoodType(AddGoodsSpuDTO typeDTO) {
        AddGoodsSpuBO spuDTO = BeanCopierUtil.copyObj(typeDTO, AddGoodsSpuBO.class);
        RestResult<Void> restResult = goodsSpuService.addGoodsSpu(spuDTO);
        if (restResult.getCode().equals(RestResultEmum.SUCCESS.code)) {
            return new RestResult<>(RestResultEmum.SUCCESS.code, RestResultEmum.SUCCESS.msg);
        }
        return new RestResult<>(RestResultEmum.SAVE_ERROR.code, RestResultEmum.SAVE_ERROR.msg);
    }

    @GetMapping("/getAll")
    RestResult<List<GetGoodsSpuVO>> getGoodSpu() {
        RestResult<List<GetGoodsSpuDO>> goodsSpu = goodsSpuService.getGoodsSpu();
        List<GetGoodsSpuVO> spuVOS = BeanCopierUtil.copyList(goodsSpu.getData(), GetGoodsSpuVO.class);
        if (spuVOS == null || spuVOS.size() == 0) {
            return new RestResult<>(RestResultEmum.GET_ERROR.code, RestResultEmum.GET_ERROR.msg);
        }
        return new RestResult<>(spuVOS);
    }
}
